// pages/new/new.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    new_content: [],
    imgurl: app.urlimg,
    // 条件切换
    conditions: 'null',
    // 价格选择框高度
    price_height: '-13',
    contraction: true,
    //用户输入的最小值
    min_interval: '',
    //用户输入的最大值
    max_interval: '',
    defaultpages: 2,
    switchs: '',
    price:'',
    category_id: 0,
    toTop: 0,
    scrollTop: {
      scroll_top: 0,
      goTop_show: false
    },
    loadHidden: false,
    loadText: "加载中...",
    toastHidden: true,
    msg: '没有更多商品了',
    price_order:0,
    moreshow:true,
    isScrollTop: false,
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [{ //假数据
    }],
		loading_ing: true,
		noset: true,
  },

 switchs: function (e) {
    var that=this
    var conditions=e.currentTarget.dataset.id;
		that.setData({
			toTop: 0,
		});
 searchData(that.data.category_id,that.data.price,conditions, e.target.dataset.current,0,'',that);

  },
  price_select:function(){
     var that=this;
    var price_order=that.data.price_order==1?2:1
		that.setData({ price_order: price_order, conditions: 3, toTop: 0,})
   searchData(that.data.category_id,that.data.price,3,that.data.currentTab,0,'',that);
  }
  ,
  // 价格选择
  price_select1:function(){
    var that=this;
    var price_order=that.data.price_order==1?2:1
    that.setData({price_order:price_order, conditions:0,})


    // 判断上下拉
   /* var contraction=that.data.contraction;
    that.setData({
     contraction: !contraction,
    
     defaultpages:2,
    })*/




     /*that.setData({
      contraction: !contraction,
    })*/
    // 获取当前位置
 /*   var price_height = parseInt(that.data.price_height);
    //下拉
    function add(){
      if (price_height <14) {
        price_height += 1;
        that.setData({
          price_height: price_height,
        })
      } else {
        clearInterval(time);
        time = null;
      }
    }
    // 上升
    function subtract() {
      if (price_height > -13) {
        price_height -= 1;
        that.setData({
          price_height: price_height,
        })
      } else {
        clearInterval(time);
        time = null;
      }
    }
    var time=setInterval(function(){
      if (that.data.contraction == false){
        add()
      }
      if (that.data.contraction == true) {
        subtract()
      }
    },15)*/
  },
  // 获取限制的价格区间
  interval: function (e) {
    var price =''
    var that=this
    if(e.currentTarget.dataset.min==500){
      price='500'
      e.currentTarget.dataset.max=''
    }else{
       price=e.currentTarget.dataset.min+','+e.currentTarget.dataset.max
    }
    searchData(that.data.category_id,price,that.data.conditions, e.target.dataset.current,e.currentTarget.dataset.min,e.currentTarget.dataset.max,that);

    // 获取当前位置
    var that=this;
    var price_height = parseInt(that.data.price_height);
    // 上升
    var time = setInterval(function () {
      if (price_height > -13) {
        price_height -= 1;
        that.setData({
          price_height: price_height,
        })
      } else {
        clearInterval(time);
        time = null;
      }
    }, 15)
  },
  /*获取用户输入的最小值 */
  user_min_interval:function(e){
    this.setData({
      min_interval:e.detail.value,
    })
  },
  /*获取用户输入的最大值 */
  user_max_interval: function (e) {
    console.log(e.detail.value);
    this.setData({
      max_interval: e.detail.value,
    })
  },
  // 确定按钮
  ko_btn:function(e){
    var that = this;
    //数据加载
var min_price=e.detail.value.user_min_interval
var max_price=e.detail.value.user_max_interval
    if(min_price==''||max_price==''){
       wx.showModal({
                          title: '提示',
                         content: '请输入价格区间',
         })
    }
var price =min_price+','+max_price
searchData(that.data.category_id,price,that.data.conditions, e.target.dataset.current,min_price,max_price,that);
    var price_height = parseInt(that.data.price_height);
    // 上升
    var time = setInterval(function () {
      if (price_height > -13) {
        price_height -= 1;
        that.setData({
          price_height: price_height,
        })
      } else {
        clearInterval(time);
        time = null;
      }
    }, 15)
  },
	scroll: function (event) {
		this.setData({
			"scrollTop.scroll_top": event.detail.scrollTop
		});
		// console.log(this.data.scrollTop);
	}, 
 scrollTop: function (e) {
    this.setData({
      toTop: 0,
    });
     },
  // scroll-view上拉加载更多
  loadMore: function () {
		var that = this;
		wx.hideNavigationBarLoading() //完成停止加载
		if (that.data.loading_ing){
			that.setData({ loadHidden: false, loading_ing: false })
    var forumPage = that.data.defaultpages
    wx.request({
      url: app.url + 'api_goods/NewGoodsV1',
      data: {
        page: forumPage,
        category_id: that.data.category_id,
          price:that.data.price,
         'switchs':that.data.conditions,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {

				wx.hideNavigationBarLoading() //完成停止加载
        if (res.data.done == true) {
					console.log(res.data.result.goods);
          var newData = that.data.new_content.concat(res.data.result.goods);

          that.setData({
            defaultpages: ++that.data.defaultpages,
            new_content: newData,
						loadHidden: true, loadText: '加载中...',
						loading_ing: true
          })
        } else {
					that.setData({ loadHidden: false, loadText: '已经到最底了', noset: false,})
					
        }
      }, complete: function () {
        // that.setData({ loadHidden: true })
        wx.hideNavigationBarLoading() //完成停止加载
        // wx.stopPullDownRefresh() //停止下拉刷新
				wx.showToast({
					title: '加载中',  //标题  
					icon: 'loading',  //图标，支持"success"、"loading"  
					duration: 500, //提示的延迟时间，单位毫秒，默认：1500 
				}) 
      }
		})

		}


  },



  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var that = this
//searchData(category_id,price,switchs,conditions,that)
 that.setData({
       loadHidden: false, 
      toTop: -10,
      loadText: '加载中...',
			loading_ing: true,
    });

searchData(e.currentTarget.id,that.data.price,that.data.conditions, e.target.dataset.current,0,'',that);
    
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },



  goodslink: function (e) {
    var fid = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + fid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
    var that = this
    that.setData({ loadHidden: false, loadText: '加载中...' })
    wx.request({
      url: app.url + 'api_goods/NewGoodsV1',
      data: {
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (json) {
        console.log(json.data.result)
        that.setData({
          new_content: json.data.result.goods,
          category: json.data.result.category, currentTab: 0,
          loadHidden: true, loadText: '加载中...'
        })
      }
    })
  },
  // onReachBottom: function () {
  //      var that = this;
  //   that.setData({ loadHidden: false, })
  //   var forumPage = that.data.defaultpages
  //   wx.request({
  //     url: app.url + 'api_goods/NewGoodsV1',
  //     data: {
  //       page: forumPage,
  //       category_id: that.data.category_id,
  //         price:that.data.price,
  //        'switchs':that.data.conditions,
  //     },
  //     header: {
  //       'content-type': 'application/json',
  //       'token': wx.getStorageSync('token_key')
  //     },
  //     success: function (res) {
  //       if (res.data.done == true) {
  //         var newData = that.data.new_content.concat(res.data.result.goods);
  //         console.log(newData)
  //         that.setData({
  //           defaultpages: ++that.data.defaultpages,
  //           new_content: newData,
  //           loadHidden: true, loadText: '加载中...',moreshow:true
  //         })
  //       } else {
  //         that.setData({ loadHidden: false, loadText: '已经到最底了',moreshow:true })
  //       }
  //     }, complete: function () {
  //       that.setData({ loadHidden: true })
  //       wx.hideNavigationBarLoading() //完成停止加载
  //       wx.stopPullDownRefresh() //停止下拉刷新
  //     }
  //   })

  // },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



  /**
   * 用户点击右上角分享
   */
 /* onShareAppMessage: function () {

  }*/
})

 /**
   * 
   */
  function searchData(category_id,price,conditions,currentTab,min_price,max_price,that){
    console.log(conditions+'conditions')
     wx.request({
                        url: app.url + 'api_goods/NewGoodsV1',
                        data: {
                          price:price,
                          price_order:that.data.price_order,
                         'switchs':conditions,
                          category_id: category_id,
                        },
                        header: {
                          'content-type': 'application/json',
                          'token':wx.getStorageSync('token_key')
                        }, success: function (json) {
                          if(json.data.done==true){

                            var price_order =that.data.price_order;
                            if(conditions<3){
                              price_order=0 //点其他价格颜色去焦点

                            }


                             that.setData({ new_content: json.data.result.goods,
                      category: json.data.result.category,
                      conditions: conditions,
                      defaultpages:2,
                      price_order:price_order,
                      price_height:-13,
                      category_id:category_id,
                      price:price,
                      currentTab:currentTab,
                      conditions:conditions,
                        defaultpages: 2,
                      min_interval:min_price,
                      max_interval:max_price,
                         loadHidden: true,
                         moreshow:false,
                      })
                       }else{
                         that.setData({new_content:null,loadHidden: true,
                             conditions:conditions,
                              category_id:category_id,
                          currentTab:currentTab,moreshow:false})
                       }
                        }
                      })
  }
